define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'user/user/index',
                    add_url: 'user/user/add',
                    edit_url: 'user/user/edit',
                    del_url: 'user/user/del',
                    multi_url: 'user/user/multi',
                    table: 'user',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'user.id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), sortable: true},
                        {field: 'group.name', title: __('Group'),visible:false},
                        {field: 'username', title: __('Username'), operate: 'LIKE'},
                        {field: 'nickname', title: __('Nickname'), operate: 'LIKE'},
                        {field: 'realname', title: '真实姓名', operate: 'LIKE'},
                        {field: 'email', title: __('Email'), operate: 'LIKE',visible: false},
                        {field: 'mobile', title: __('Mobile'), operate: 'LIKE'},
                        {field: 'avatar', title: __('Avatar'), formatter: Table.api.formatter.image, operate: false},
                        {field: 'level', title: __('Level'), operate: 'BETWEEN', sortable: true, visible: false},
                        {field: 'cash_money', title: __('CashMoney'), operate: 'LIKE', sortable: true},
                        {field: 'cloud_money', title: __('CloudMoney'), operate: 'LIKE', sortable: true},
                        {field: 'cloud_money_release', title: '认筹云币', operate: 'LIKE', sortable: true},
                        {field: 'cloud_money_register', title: '注册赠送云币', operate: 'LIKE', sortable: true},
                        {field: 'cloud_money_freeze', title: '冻结云币', operate: 'LIKE', sortable: true},
                        {field: 'gender', title: __('Gender'), visible: false, searchList: {1: __('Male'), 0: __('Female')}},
                        {field: 'score', title: __('Score'), operate: 'BETWEEN', sortable: true,visible: false},
                        {field: 'invite_code', title: __('InviteCode'), operate: 'BETWEEN'},
                        {field: 'successions', title: __('Successions'), visible: false, operate: 'BETWEEN', sortable: true},
                        {field: 'maxsuccessions', title: __('Maxsuccessions'), visible: false, operate: 'BETWEEN', sortable: true},
                        {field: 'logintime', title: __('Logintime'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true, visible: false},
                        {field: 'loginip', title: __('Loginip'), formatter: Table.api.formatter.search, visible: false},
                        {field: 'jointime', title: __('Jointime'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true, visible: false},
                        {field: 'joinip', title: __('Joinip'), formatter: Table.api.formatter.search, visible: false},
                        {field: 'status', title: __('Status'), formatter: Table.api.formatter.status, searchList: {normal: __('Normal'), hidden: __('Hidden')}, visible: false},
                        {field: 'enable', title: __('EnableStatus'), formatter: this.enable, searchList: {1: __('Enable'), 0: __('Disable')}},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        },
        enable: function (value, row, index) {
            //颜色状态数组,可使用red/yellow/aqua/blue/navy/teal/olive/lime/fuchsia/purple/maroon
            var colorArr = {1: 'success', 0: 'grey', deleted: 'danger', locked: 'info'};
            //如果字段列有定义custom
            if (typeof this.custom !== 'undefined') {
                colorArr = $.extend(colorArr, this.custom);
            }
            value = value === null ? '' : value.toString();
            var color = value && typeof colorArr[value] !== 'undefined' ? colorArr[value] : 'primary';
            var newValue = value.charAt(0).toUpperCase() + value.slice(1);
            var valueText = {1:'激活',0:'未激活'};
            //渲染状态
            var html = '<span class="text-' + color + '"><i class="fa fa-circle"></i> ' + valueText[newValue] + '</span>';
            if (this.operate != false) {
                html = '<a href="javascript:;" class="searchit" data-toggle="tooltip" title="' + __('Click to search %s', valueText[newValue]) + '" data-field="' + this.field + '" data-value="' + value + '">' + html + '</a>';
            }
            return html;
        },
    };
    return Controller;
});